
package protocol.transactions;

import preferMgr.XonePrefHelper;
import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.APPSetting;
import android.content.Context;
import app.BaseApplication;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;
import com.netease.log.NTLog;

public class SetAppSettingTransaction extends XoneBaseTransaction {

    APPSetting mSetting;

    public SetAppSettingTransaction() {
        super(TRANSACTION_TYPE_SET_APP_SETTING);
        Context context = BaseApplication.getAppInstance();
        mSetting = new APPSetting();
        mSetting.setAtMe(XonePrefHelper.getAtMeOn(context));
        mSetting.setBanPush(!XonePrefHelper.getPushOn(context));
        mSetting.setFriendDynamic(XonePrefHelper.getNewFriendDynamicOn(context));
        mSetting.setNewComment(XonePrefHelper.getNewCommentOn(context));
        mSetting.setNewFans(XonePrefHelper.getNewFansOn(context));
        mSetting.setNewLetter(XonePrefHelper.getNewMessageOn(context));
        mSetting.setReceiveAllComment(XonePrefHelper.getReceiveAllComment(context));
        mSetting.setReceiveAllMessage(XonePrefHelper.getReceiveAllMessage(context));
        mSetting.setShock(XonePrefHelper.getShockOn(context));
        mSetting.setSound(XonePrefHelper.getSoundOn(context));
        mSetting.setTimestamp(XonePrefHelper.getSettingUpdateTime(context));
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj != null && obj instanceof JsonElement) {
            JsonElement json = ((JsonElement)obj).getAsJsonObject().get("config");
            if (json != null) {
                Gson gson = new Gson();
                APPSetting setting = gson.fromJson(json, APPSetting.class);
                if (setting != null) {
                    Context context = BaseApplication.getAppInstance();
                        XonePrefHelper.putSettingUpdateTime(context, setting.getTimestamp());
                        XonePrefHelper.putAtMeOn(context, setting.isAtMe());
                        XonePrefHelper.putNewCommentOn(context, setting.isNewComment());
                        XonePrefHelper.putNewFansOn(context, setting.isNewFans());
                        XonePrefHelper.putNewFriendDynamicOn(context, setting.isFriendDynamic());
                        XonePrefHelper.putNewMessageOn(context, setting.isNewLetter());
                        XonePrefHelper.putPushOn(context, !setting.isBanPush());
                        XonePrefHelper.putReceiveAllComment(context, setting.isReceiveAllComment());
                        XonePrefHelper.putReceiveAllMessage(context, setting.isReceiveAllMessage());
                        XonePrefHelper.putShockOn(context, setting.isShock());
                        XonePrefHelper.putSoundOn(context, setting.isSound());
                        NTLog.e("SetAppSettingTransaction","SetApp Success setting.isBanPush() is " + setting.isBanPush());
                        NTLog.e("SetAppSettingTransaction","SetApp Success setting.getTimestamp() is " + setting.getTimestamp());
                    
                }
            }
        }
        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, mSetting);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createSetAppSettingRequest(mSetting);
        sendRequest(request);
    }

}
